home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 5.1 KB | 291 lines | [TEXT/MPS ] |
- /*
- File: ObjectList.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __OBJECTLIST__
- #include "ObjectList.h"
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __DEBUGASSERT__
- #include "DebugAssert.h"
- #endif
-
- #ifndef __DEBUGGINGGEAR__
- #include "DebuggingGear.h"
- #endif
-
- #pragma segment ObjectList
-
- void* const kBadObject = (void*) 0xFFFFFFFF;
-
- extern void BreakStr ( const Str255 str );
-
- /***********************************|****************************************/
-
- void
- TObjectList::SetSize ( unsigned long objects )
- {
- if ( objects != fCount )
- {
- ::SetHandleSize ( (Handle) fList, objects * sizeof ( void* ) );
- FAILOSErr ( MemError () );
- fCount = objects;
- }
- }
-
- /***********************************|****************************************/
-
- TObjectList::TObjectList ( Boolean ownsObjects ):
- fList ( (void***) FAILNewHandle ( 0 ) ),
- fCount ( 0 ),
- fOwnsObjects ( ownsObjects )
- {
- }
-
- /***********************************|****************************************/
-
- TObjectList::~TObjectList ()
- {
- if ( fCount > 0 && fOwnsObjects )
- DeleteAll ();
-
- ::DeallocateHandle ( (Handle) fList );
- }
-
- /***********************************|****************************************/
-
- void
- TObjectList::SetOwnsObjects ( Boolean ownsObjects )
- {
- fOwnsObjects = ownsObjects;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TObjectList::GetOwnsObjects () const
- {
- return fOwnsObjects;
- }
-
- /***********************************|****************************************/
-
- unsigned long
- TObjectList::Find ( const void* object ) const
- {
- unsigned long index = fCount;
-
- while ( index-- > 0 )
- if ( object == (*fList) [ index ] )
- return index + 1;
-
- return 0;
- }
-
- /***********************************|****************************************/
-
- void*
- TObjectList::Get ( unsigned long index ) const
- {
- if ( IsValidIndex ( index ) )
- {
- return (*fList) [ --index ];
- }
- else
- {
- #if debug
- BreakStr ( "\pinvalid index" );
- #endif
- return nil;
- }
- }
-
- /***********************************|****************************************/
-
- void*
- TObjectList::operator [] ( unsigned long index ) const
- {
- if ( IsValidIndex ( index ) )
- {
- return (*fList) [ --index ];
- }
- else
- {
- #if debug
- BreakStr ( "\pinvalid index" );
- #endif
- return nil;
- }
- }
-
- /***********************************|****************************************/
-
- void
- TObjectList::Insert ( unsigned long index, void* object )
- {
- #if debug
- if ( index == 0 )
- {
- BreakStr ( "\pzero user index" );
- return;
- }
- #endif
-
- if ( index >= fCount )
- SetSize ( index );
-
- (*fList) [ --index ] = object;
- }
-
- /***********************************|****************************************/
-
- void*
- TObjectList::Remove ( unsigned long index )
- {
- #if debug
- if ( !IsValidIndex ( index ) )
- {
- BreakStr ( "\pbad index" );
- return nil;
- }
- #endif
-
- void* object = (*fList) [ --index ];
-
- #if debug
- if ( object == nil )
- {
- BreakStr ( "\pnil object" );
- return nil;
- }
-
- if ( object == kBadObject )
- {
- BreakStr ( "\pbad object" );
- return nil;
- }
-
- (*fList) [ index ] = kBadObject;
- #endif
-
- unsigned long newCount = fCount - 1;
-
- if ( index < newCount )
- ::BlockMove ( (*fList) + index + 1, (*fList) + index, ( newCount - index ) * sizeof ( void* ) );
-
- SetSize ( newCount );
-
- return object;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TObjectList::Delete ( unsigned long index )
- {
- void* object = Remove ( index );
-
- if ( object )
- {
- DeleteObject ( object );
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /***********************************|****************************************/
-
- Boolean
- TObjectList::Remove ( const void* object )
- {
- unsigned long index = Find ( object );
-
- if ( index > 0 )
- {
- Remove ( index );
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /***********************************|****************************************/
-
- void
- TObjectList::RemoveAll ()
- {
- SetSize ( 0 );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TObjectList::Delete ( void* object )
- {
- Boolean removed = Remove ( object );
-
- if ( removed )
- DeleteObject ( object );
-
- return removed;
- }
-
- /***********************************|****************************************/
-
- void
- TObjectList::DeleteAll ()
- {
- for ( unsigned long index = 0; index < fCount; index++ )
- DeleteObject ( (*fList) [ index ] );
-
- SetSize ( 0 );
- }
-
- /***********************************|****************************************/
-
- void
- TObjectList::DeleteObject ( void* object ) const
- {
- delete object;
- }
-
- /***********************************|****************************************/
-
- ostream&
- TObjectList::operator >> ( ostream& s ) const
- {
- // subclasses should stream their name, then call this method
-
- s << " @ " << (void*) this << ": (" << Count() << ") ";
-
- for ( unsigned long index = 0; index < fCount; index++ )
- {
- if ( index > 0 )
- s << ",";
-
- s << Get ( index + 1 );
- }
-
- return s;
- }
-
- /***********************************|****************************************/
-